#include"Sock.hpp"

void Usage(std::string s)
{
    std::cout <<"Usage: "<<s <<"port"<<std::endl;
}

void* Routine(void* arg)
{
	pthread_detach(pthread_self());//线程分离
	int fd = *(int*)arg;
	delete (int*)arg;
	while (1)
    {
		std::cout << "Runing"<<std::endl;
		sleep(2);
	}
	return nullptr;
}

//./server port
int main(int args,char* argv[])
{
    if(args!=2)
    {
        Usage(argv[0]);
        exit(1);
    }
    int listen_sock = Sock::Socket();//创建监听套接字
    Sock::Bind(listen_sock,atoi(argv[1]));//绑定
    Sock::Listen(listen_sock);//监听

    for(;;)
    {
        int sock = Sock::Accept(listen_sock);//获取新连接
        std::cout << "get a new link: " << sock << std::endl;

        //创建新线程提供服务
        int* p = new int(sock);
		
		pthread_t tid;
		pthread_create(&tid, nullptr, Routine, (void*)p);
    }
    return 0;
}